package cn.wolfcode.shop.controller;


import cn.wolfcode.shop.api.domain.*;
import cn.wolfcode.shop.api.page.PageResult;
import cn.wolfcode.shop.api.query.ProductListQueryObject;
import cn.wolfcode.shop.api.service.*;
import cn.wolfcode.shop.api.vo.*;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;


@Api(value = "通用页面接口", description = "提供通用页面的信息及相关服务")
@RestController
@RequestMapping("api/commonPage")
public class CommonPageController {
    @Reference
    private ICatalogService catalogService;
    @Reference
    private IProductService productService;
    @Reference
    private ISkuPropertyService skuPropertyService;
    @Reference
    private IProductSkuService productSkuService;
    @Reference
    private ISkuPropertyValueService skuPropertyValueService;



    @ApiOperation(value = "商品分类列表", notes = "根据传入的id,获取下一层的商品分类,id为null时返回首层商品列表")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "商品分类ID", paramType = "path", dataType = "Long")
    )
    @RequestMapping(value = {"categroy/{id}", "categroy"}, method = RequestMethod.GET)
    public JsonResultVo catalogList(@PathVariable(value = "id", required = false) Long parentId) {
        JsonResultVo resultVo = new JsonResultVo();
        try {
            List<Catalog> catalogs = null;
            List<CatalogInterfaceVo> catalogInterfaceVos = new ArrayList<>();
            if (parentId == null) {
                catalogs = catalogService.selecRootTree();
            } else {
                catalogs = catalogService.selectSonTree(parentId);
            }
            for (Catalog catalog : catalogs) {
                catalogInterfaceVos.add(new CatalogInterfaceVo(catalog.getId(), catalog.getName()));
            }
            resultVo.setResult(catalogInterfaceVos);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setSuccess(false);
            resultVo.setErrorMsg(e.getMessage());
        }
        return resultVo;
    }

    @ApiOperation(value = "商品信息", notes = "根据传入的id,获取相应的商品信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "商品ID", paramType = "path", dataType = "Long", required = true)
    )
    @RequestMapping(value = "product_info/{id}", method = RequestMethod.GET)
    public JsonResultVo productInfo(@PathVariable(value = "id") Long productId) {
        JsonResultVo resultVo = new JsonResultVo();
        try {
            Product product = productService.getProductInfoVo(productId);
            //设置返回信息中的商品信息
            ProductInfoInterfaceVo productInfo = new ProductInfoInterfaceVo();
            productInfo.setProductId(productId);
            productInfo.setProductName(product.getName());
            productInfo.setMarketPrice(product.getMarketPrice());
            if (product.getBasePrice() != null) {
                productInfo.setSalePrice(product.getBasePrice());
            } else {
                productInfo.setSalePrice(productSkuService.selectMinPriceByProductId(productId));
            }
            //设置返回信息中的sku属性信息
            List<SkuPropertyInterfaceVo> skuPropertiesList = new ArrayList<>();
            List<SkuProperty> skuProperties = skuPropertyService.getSkuProperty(product.getCatalog().getId());
            for (SkuProperty property : skuProperties) {
                SkuPropertyInterfaceVo skuProperty = new SkuPropertyInterfaceVo();
                skuProperty.setName(property.getName());
                Long propertyId = property.getId();
                skuProperty.setSkuPropId(propertyId);
                List<SkuPropertyValueVo> valueVos = new ArrayList<>();
                List<SkuPropertyValue> values = skuPropertyValueService.selectVoBySkuPropertyId(propertyId);
                for (SkuPropertyValue value : values) {
                    SkuPropertyValueVo vo = new SkuPropertyValueVo();
                    vo.setId(value.getId());
                    vo.setValue(value.getValue());
                    valueVos.add(vo);
                }
                skuProperty.setValues(valueVos);
                skuPropertiesList.add(skuProperty);
            }
            //设置返回信息中的sku信息
            List<SkuInterfaceVo> skuInterfaceVos = new ArrayList<>();
            List<ProductSku> productSkus = productSkuService.selectSkuByProductId(productId);
            for (ProductSku productSku : productSkus) {
                SkuInterfaceVo skuInterfaceVo = new SkuInterfaceVo();
                skuInterfaceVo.setSkuId(productSku.getId());
                skuInterfaceVo.setCode(productSku.getCode());
                skuInterfaceVo.setSkuPrice(productSku.getPrice());
                List<ProductSkuProperty> productSkuPropertyList = productSku.getProductSkuPropertyList();
                List<ProductSkuPropertyVo> productSkuPropertyVos = new ArrayList<>();
                for (ProductSkuProperty productSkuProperty : productSkuPropertyList) {
                    ProductSkuPropertyVo vo = new ProductSkuPropertyVo();
                    vo.setId(productSkuProperty.getSkuPropertyId());
                    vo.setValue(productSkuProperty.getValue());
                    productSkuPropertyVos.add(vo);
                }
                skuInterfaceVo.setSkuPropValueCombination(productSkuPropertyVos);
                skuInterfaceVos.add(skuInterfaceVo);
            }
            List<Object> res = new ArrayList<>();
            res.add(productInfo);
            res.add(skuPropertiesList);
            res.add(skuInterfaceVos);
            resultVo.setResult(res);

        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setSuccess(false);
            resultVo.setErrorMsg(e.getMessage());
        }
        return resultVo;
    }


    @ApiOperation(value = "商品列表", notes = "根据选择的条件,获取对应的商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "货品关键字", paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "categoryId", value = "货品所属分类", paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "sortType", value = "排序条件,1:价格从高到低,2:价格从低到高", paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "minPrice", value = "最低价格", paramType = "form", dataType = "Number"),
            @ApiImplicitParam(name = "maxPrice", value = "最高价格", paramType = "form", dataType = "Number"),
            @ApiImplicitParam(name = "brandId", value = "品牌id", paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "product",method = RequestMethod.POST)
    public JsonResultVo productList(@ApiIgnore ProductListQueryObject qo){
        JsonResultVo resultVo = new JsonResultVo();
        List<ProductListVo> productListVoList = new ArrayList<>();
        try {
            PageResult pageResult = productService.queryProductListInterface(qo);
            List<Product> productList = (List<Product>) pageResult.getList();
            for (Product product : productList) {
                ProductListVo productListVo = new ProductListVo();
                productListVo.setProductId(product.getId());
                productListVo.setProductName(product.getName());
                productListVo.setSalePrice(product.getMarketPrice());
                productListVo.setProductImg(product.getImage());
                productListVoList.add(productListVo);
            }
            resultVo.setResult(productListVoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setSuccess(false);
            resultVo.setErrorMsg(e.getMessage());
        }
        return resultVo;
    }

}
